<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        原料数据字典列表
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row" style="margin-bottom: 10px">
                            <div class="form-inline">
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>分类</label>
                                    <select name="mdPid"
                                            class="form-control">
                                        <option value="1">板材</option>
                                        <option value="2">型材</option>
                                        <option value="3">通用</option>
                                        <option value="" selected="selected">全部</option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>类别</label>
                                    <select name="mdTypeNum"
                                            class="form-control">
                                        <option value="1000">类型</option>
                                        <option value="2000">材质</option>
                                        <option value="3000">规格</option>
                                        <option value="" selected="selected">全部</option>
                                    </select>
                                </div>
                                <div class="form-group" style="margin-right: 10px;">
                                    <label>内容</label>
                                    <input type="text" name="mdName"
                                           class="form-control"
                                           placeholder="请输入内容">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">搜索</button>
                                <a shiro:hasPermission="basic:rawmatinfo:edit"
                                   th:href="@{/basic/materialdictionary/create}"
                                   class="btn btn-info pull-right">添加</a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-7">
                            <table id="bootstrap-table"></table>
                        </div>
                        <div class="col-md-5">
                            <div style="position:relative;" id="right_div">
                                <form id="materialForm" class="form-horizontal">
                                    <input type="hidden" id="mdId" name="mdId">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="mdPid" class="col-sm-2 control-label">分类</label>
                                            <div class="col-sm-6">
                                                <select id="mdPid" class="form-control"
                                                        name="mdPid" style="width: 100%;">
                                                    <option value="">请选择</option>
                                                    <option value="1">板材</option>
                                                    <option value="2">型材</option>
                                                    <option value="3">通用</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="mdTypeNum" class="col-sm-2 control-label">类别</label>
                                            <div class="col-sm-6">
                                                <select id="mdTypeNum" class="form-control"
                                                        name="mdTypeNum" style="width: 100%;" disabled="disabled">
                                                    <option value="">请选择</option>
                                                    <option value="1000">类型</option>
                                                    <option value="2000">材质</option>
                                                    <option value="3000">规格</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">内容</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"
                                                       id="mdName" name="mdName" placeholder="请输入内容" autocomplete="off">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">编码</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control"
                                                       id="mdMatCode" name="mdMatCode" placeholder="请输入编码"
                                                       disabled="disabled">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="mdFormulaType" class="col-sm-2 control-label">重量公式</label>
                                            <div class="col-sm-10">
                                                <select id="mdFormulaType" class="form-control"
                                                        name="mdFormulaType"
                                                        style="width: 100%;">
                                                    <option value="">请选择</option>
                                                    <option value="">&nbsp;</option>
                                                    <option value="1">
                                                        （规格*宽度*长度*数量*7.85/1000）-（(规格-2*米重/厚度)*（宽度-2*米重/厚度）*7.85/1000*数量*长度
                                                    </option>
                                                    <option value="2">规格*规格*长度*数量*0.00617</option>
                                                    <option value="3">长度*数量*米重/厚度</option>
                                                    <option value="4">（规格-宽度）*宽度*0.02466*长度*数量</option>
                                                    <option value="5">规格*宽度*长度*2.7/1000000*数量</option>
                                                    <option value="6">规格*宽度*长度*7.85/1000000*数量</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="box-footer">
                                        <button shiro:hasPermission="basic:rawmatinfo:edit" type="button"
                                                id="btn_edit" class="btn btn-info pull-right" disabled>
                                            修改
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>

<script th:inline="javascript">

    function updateMaterialDictionary() {
        js.post({
            url: ctx + "basic/materialdictionary/update",
            data: $("#materialForm").serialize(),
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
                js.modal.closeLoading();
            }
        })
        js.validate.reset("materialForm")
    }

    function updateStatus(id) {
        js.modal.confirm("确定删除这个原料数据字典吗？", function (result) {
            js.post({
                url: ctx + "basic/materialdictionary/del",
                data: {
                    mdId: id
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        if (id == $("#mdId").val()) {
                            $("#mdId").val('');
                            $("#mdMatCode").val('');
                            $("#mdPid").val('');
                            $("#mdTypeNum").val('');
                            $("#mdName").val('');
                            $("#btn_edit").attr("disabled", true);
                        }
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    $(function () {
        $("title").html($("title").html()+" —— 原料数据字典维护");
        /** 加载列表 */
        table = js.table.init({
            url: ctx + "basic/materialdictionary/list",
            showExport: false,
            columns: [
                {
                    title: '序号', field: '', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '分类', field: 'parentName'},
                {title: '类别', field: 'typeName'},
                {title: '内容', field: 'mdName'},
                {title: '编码', field: 'mdMatCode'},
                {title: '状态', field: 'statusName'},
                {
                    title: '操作', width: '60',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("basic:rawmatinfo:edit")) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="updateStatus(' + row.mdId + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /** 行点击事件 */
        table.on('click-row.bs.table', function (e, row, tr) {

            $("#mdId").val(row.mdId);
            $("#mdMatCode").val(row.mdMatCode);
            $("#mdName").val(row.mdName);
            $("#mdPid").val(row.mdPid);
            $("#mdTypeNum").val(row.mdTypeNum);
            $("#btn_edit").removeAttr("disabled");

            if (row.mdTypeNum === 1000) {

                $("#mdFormulaType").removeAttr("disabled");
                $("#mdFormulaType").val(data.mdFormulaType).trigger("change");
            } else {

                $("#mdFormulaType").removeAttr("disabled");
                $("#mdFormulaType").val("");
                $("#mdFormulaType").attr("disabled", "disabled")
            }
            js.validate.reset("materialForm")

        });

        // 验证初始化
        js.validate.init("materialForm", {
            fields: {
                mdId: {validators: {notEmpty: {message: '请选择左侧要修改的数据'}}},
                mdName: {validators: {notEmpty: {message: '请输入内容'}}},
                mdTypeNum: {validators: {notEmpty: {message: '请选择类别'}}},
                mdMatCode: {validators: {notEmpty: {message: '请输入内容简码'}}},
                mdPid: {validators: {notEmpty: {message: '请选择分类'}}}
            }
        });

        // 修改物料字典名称
        $("#btn_edit").click(function () {
            if (js.validate.isValid("materialForm")) {
                js.post({
                    url: ctx + "basic/materialdictionary/usedcount",
                    data: {
                        mdId: $("#mdId").val()
                    },
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            if (parseInt(result.data) > 0) {
                                js.modal.confirm("该数据已经被原料所使用，修改后过往历史记录将同步改变,是否继续修改?", function () {
                                    updateMaterialDictionary();
                                })
                            } else {
                                updateMaterialDictionary();
                            }
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                });
            }
        });

        window.onscroll = function () {
            var top = document.body.scrollTop || document.documentElement.scrollTop;
            if (top > 100) {
                $("#right_div").css('top', top - 100);
            } else {
                $("#right_div").css('top', 0);
            }
        }
    });

</script>

</body>
</html>